﻿namespace WinRT.Triggers
{
    #region Using directives

    using Windows.UI.Xaml;

    #endregion

    /// <summary>
    /// An action that changes the current state associated to the <see cref="VisualStateManager"/>.
    /// </summary>
    public class GotoStateAction : TriggerAction
    {
        #region Static Fields

        /// <summary>
        /// The StateName dependency property of type <see cref="string"/>.
        /// </summary>
        public static readonly DependencyProperty StateNameProperty = DependencyProperty.Register(
            "StateName", typeof(string), typeof(GotoStateAction), new PropertyMetadata(DependencyProperty.UnsetValue));

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the name of the state to change to when the action is invoked.
        /// </summary>
        public string StateName
        {
            get
            {
                return (string)this.GetValue(StateNameProperty);
            }

            set
            {
                this.SetValue(StateNameProperty, value);
            }
        }

        #endregion

        #region Methods

        /// <inheritdoc />
        protected override void DoInvoke(object eventData)
        {
            if (this.AssociatedObject != null && !string.IsNullOrEmpty(this.StateName))
            {
                CustomVisualStateManager.GoToState(this.AssociatedObject, this.StateName);
            }
        }

        #endregion
    }
}